package ui.handlers;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

import core.MenuItemType;

import ui.HomeScreen;
import ui.ManagerHomeScreen;

import ui.ViewMenuScreen;
import ui.EditMenuScreen;
import ui.SetDailySpecialScreen;
import ui.KioskCustomerScreen;

public class MenuHandler extends BaseHandler implements ActionListener {

	private HomeScreen homeScreen = null;
	private ManagerHomeScreen managerHomeScreen = null;
	private EditMenuScreen editMenuScreen = null;
	private SetDailySpecialScreen setDailySpecialScreen = null;
	private KioskCustomerScreen kioskCustomerScrn = null; 
	
	public MenuHandler(HomeScreen hs)
	{ homeScreen = hs; }
	
	public MenuHandler(ManagerHomeScreen mhs)
	{ managerHomeScreen = mhs; }
	
	public MenuHandler(EditMenuScreen cms)
	{ editMenuScreen = cms; }
	
	public MenuHandler(SetDailySpecialScreen sdss)
	{ setDailySpecialScreen = sdss; }
	
	public MenuHandler(KioskCustomerScreen kcs)
	{ kioskCustomerScrn = kcs; }
	
	public void actionPerformed(ActionEvent e)
	{
		Object src = e.getSource();
		
		if (homeScreen != null)
		{
			if (src == homeScreen.getViewMenuButton())
			{ viewMenu(); }
		}
		
		if (managerHomeScreen != null)
		{
			if (src == managerHomeScreen.getEditMenuButton())
			{ new EditMenuScreen(); }
			else if (src == managerHomeScreen.getSetDailySpecialButton())
			{ new SetDailySpecialScreen(); }
		}
		
		if (editMenuScreen != null)
		{
			if (src == editMenuScreen.getAddItemButton())
			{ try {
				submit();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} }
			else if (src == editMenuScreen.getClearButton())
			{ clear(); }
		}
		
		if (setDailySpecialScreen != null)
		{
			if (src == setDailySpecialScreen.getSetButton())
			{ setDailySpecial(); }
		}
		
		if(kioskCustomerScrn != null)
		{
			if (src == kioskCustomerScrn.getViewMenuButton())
			{ new ViewMenuScreen(); }
		}
	}
	
	private void viewMenu()
	{ new ViewMenuScreen(); }
	
	private void submit() throws RemoteException
	{
		String name = editMenuScreen.getItemNameText();
		String desc = editMenuScreen.getItemDescText();
		String price = editMenuScreen.getItemPriceText();	
		MenuItemType type = editMenuScreen.getItemType();
		editMenuScreen.getStore().addMenuItem(name, price, type, desc);
		editMenuScreen.loadMainContent();
		clear();
	}
	
	private void clear()
	{
		editMenuScreen.clearItemDescText();
		editMenuScreen.clearItemPriceText();
		editMenuScreen.cleartItemNameText();
	}

	private void setDailySpecial()
	{
		String name = setDailySpecialScreen.getName();
		String price = setDailySpecialScreen.getPrice();
		MenuItemType type = setDailySpecialScreen.getSelectedMenuItemType();
		String description = setDailySpecialScreen.getDescription();
		try {
			setDailySpecialScreen.getStore().setDailySpecial(name, price, type, description);
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		close(setDailySpecialScreen.getMainFrame());
	}
}